/**
 *
 */
package chap1;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Hiroshi.Nakai
 *
 */
public class Sample1_1 extends HttpServlet {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	private String host = null;

	/* (非 Javadoc)
	 * @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig)
	 *
	 * サーブレットがインスタンス化された時のみ実行される。
	 */
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);

		System.out.println("called init(...)");
	}



	/* (非 Javadoc)
	 * @see javax.servlet.GenericServlet#init()
	 *
	 * サーブレットがインスタンス化された時のみ実行される。
	 */
	@Override
	public void init() throws ServletException {

		ServletContext context = getServletContext();
		ServletConfig config = getServletConfig();

		host = context.getInitParameter("host");


		System.out.println("called init()");
	}

	/* (非 Javadoc)
	 * @see javax.servlet.GenericServlet#destroy()
	 */
	@Override
	public void destroy() {
		System.out.println("called destroy()");
	}



	/* (非 Javadoc)
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		// 日本語処理
		req.setCharacterEncoding("Windows-31J");
		resp.setCharacterEncoding("Windows-31J");

		PrintWriter out = resp.getWriter();

		out.println("<html>");
		out.println("<body>");
		out.println("<h1>Hello !</h1>");
		out.println("<table table width='500' border='1' cellspacing='0' cellpadding='2'>");
		Enumeration<?> e = req.getHeaderNames();
		while (e.hasMoreElements()) {

            String strName = (String)e.nextElement();
            String strVal  = (String)req.getHeader(strName);
            int header = req.getIntHeader("Keep-Alive");

            out.println("<tr>");
            out.println(" <td>" + strName + "</td>");
            out.println(" <td>" + strVal + "</td>");
            out.println(" <td>" + header +  "</td>");

            out.println("</tr>");
		}
		out.println("</table>");
		out.println("<br>");

		// サーブレットバージョン取得
		ServletContext context = getServletContext();

		out.println("<table table width='500' border='1' cellspacing='0' cellpadding='2'>");
        out.println("<tr>");
        out.println(" <td>サーブレットバージョン</td>");
        out.println(" <td>サーブレット情報</td>");
        out.println(" <td>リダイレクトホスト</td>");
        out.println("</tr>");
        out.println("<tr>");
        out.println(" <td>" + context.getMajorVersion() + "</td>");
        out.println(" <td>" + context.getServerInfo() + "</td>");
        out.println(" <td>" + host + "</td>");
        out.println("</tr>");
		out.println("</table>");
		out.println("<br>");

		out.println("</body>");
		out.println("</html>");

		System.out.println("called doGet(...)");

	}



	/* (非 Javadoc)
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		// 日本語処理
		req.setCharacterEncoding("Windows-31J");

		// フォーム情報を獲得する（重要：name属性でしか取得不可？）
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String[] chkinterests = req.getParameterValues("chkinterests");
		String[] rdointerests = req.getParameterValues("rdointerests");


		// 日本語処理
		resp.setContentType("text/html; charset=Windows-31J");

		PrintWriter out = resp.getWriter();
		out.println("<html>");
		out.println("<body>");

		out.println("<h1>" + "名前：" + name + "</h1>");
		out.println("<h1>" + "年齢：" + age + "</h1>");

		out.println("<br>");
		out.println("趣味");
		out.println("<br>");
		if (chkinterests != null) {
			for (String s:chkinterests) {
				out.println("<h1>" + s + "</h1>");
			}
		}
		out.println("<br>");
		if (rdointerests != null) {
			for (String s:rdointerests) {
				out.println("<h1>" + s + "</h1>");
			}
		}
		out.println("<br>");
		String btn = req.getParameter("btnHirorosoft");
		out.println("<h1>" + "ボタン名：" + btn + "</h1>");
		out.println("<br>");
		out.println("</body>");
		out.println("</html>");

		// 転送
		getServletContext().getRequestDispatcher("/rd1").forward(req, resp);


		System.out.println("called doPost(...)");


	}




}
